package Demo;

import javax.imageio.IIOException;
import java.io.*;

public class Test2 {
    //以字符流写数据和以字符流读数据
    public static void main(String[] args) {
        Writer writer=null;
        try{
            writer=new FileWriter("C:\\java2024\\20241126\\text2.txt");
            writer.write("开心生活每一天！！！");
            writer.write("高高兴兴过大年！！！");
            writer.write("你好");
        }catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(writer!=null){
                try {
                    writer.close();;
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        Reader reader=null;
        try{
            reader=new FileReader("C:\\java2024\\20241126\\test2.txt");
            char[] b=new char[1024];
            int len;
            while((len=reader.read(b))!=-1){
                System.out.println(new String(b,0,len));
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try {
                    reader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}
